<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use Validator;

class CommonController extends Controller
{
    protected $view;

    protected $namespace;
    protected $controller;
    protected $method;
    protected $router;

    protected $data = [];

    protected $uploadError;

    public function __construct()
    {
        $route = $this->getRoute();

        $this->namespace = $route['namespace'];
        $this->controller = $route['controller'];
        $this->method = $route['method'];
        $this->router = $route['router'];
    }

    private function getRoute()
    {
        $action = \Route::currentRouteAction();
        $actions = explode('\\', $action);
        $len = count($actions);
        $namespace = $actions[$len - 2];
        $_controller = $actions[$len - 1];
        $_controller = explode('@', $_controller);
        $controller = $_controller[0];
        $method = $_controller[1];
        $namespace = strtolower($namespace);
        $controller = strtolower(substr($controller, 0, -10));
        $method = strtolower($method);
        $router = $namespace.'.'.$controller.'.'.$method;

        return ['namespace' => $namespace, 'controller' => $controller, 'method' => $method, 'router' => $router];
    }

    // public function view($view = '')
    // {
    //     if ($view) {
    //         $view = view($view);
    //     } else {
    //         $view = view($this->router);
    //     }
    //     $this->view = $view;
    // }

    public function assign($key, $value)
    {
        $this->data[$key] = $value;
    }

    public function fetch($view = '')
    {
        if ($view) {
            $view = view($view);
        } else {
            $view = view($this->router);
        }
        if ($this->data)
        {
            $data = $this->data;
            foreach($data as $k => $v)
            {
                $view->with($k, $v);
            }
        }
        return $view;
    }

    public function assignSiteInfo($page)
    {
        $siteinfo = [
            'stitle' => '',
            'title' => '海螺云文档 - 开发者文档管理平台',
            'keywords' => '海螺云文档,文档,文档管理,开发者,API接口,云文档',
            'description' => '海螺云文档是一款轻便、简洁的在线协作文档工具，PC端和移动端全覆盖，支持多人同时对文档编辑和评论，让你与他人轻松完成协作撰稿、方案讨论、会议记录和资料共享等工作。',
        ];

        switch ($page) {
            case 'index/index':
                $siteinfo['stitle'] = '首页';
                break;
            case 'user/login':
                $siteinfo['stitle'] = '登录';
                break;
            case 'user/register':
                $siteinfo['stitle'] = '注册';
                break;
            case 'user/index':
                $siteinfo['stitle'] = '个人中心';
                break;
            case 'user/profile':
                $siteinfo['stitle'] = '个人资料';
                break;
            case 'project/index':
                $siteinfo['stitle'] = '项目列表';
                break;
            default:
                break;
        }
        $siteinfo['title'] = $siteinfo['stitle'].' - '.$siteinfo['title'];
        $this->assign('siteinfo', $siteinfo);

        return $siteinfo;
    }

    public function viewa($siteinfo = [], $view = null)
    {
        if ($siteinfo) {
            $view = $view->with('siteinfo', $siteinfo);
        } else {
            $siteinfo = [];
            switch ($router) {
                case 'index.index.index':
                    $siteinfo['title'] = '首页';
                    break;
                case 'index.index.login':
                    $siteinfo['title'] = '登录';
                    break;
                case 'index.index.register':
                    $siteinfo['title'] = '注册';
                    break;
                default:
                    break;
            }
            $siteinfo['keywords'] = '网站关键词';
            $siteinfo['description'] = '网站描述';
            $view = $view->with('siteinfo', $siteinfo);
        }

        return $view;
    }

    public function output($code = 0, $msg = '', $data = [])
    {
        if (is_object($msg)) {
            // $msg = $msg[0];
            foreach ($msg->all() as $message) {
                $msg = $message;
                break;
            }
        } elseif (!$msg && $code) {
            $msg = lang($code);
        }

        $resp = [];
        $resp['code'] = $code;
        $resp['msg'] = $msg;
        $resp['data'] = $data;
        $resp['time'] = time();

        return response()->json($resp);
    }

    public function controller()
    {
        return $this->controller;
    }

    public function namespace()
    {
        return $this->namespace;
    }

    public function method()
    {
        return $this->method();
    }

    public function upload($file = '')
    {
        $request = request();
        $data = $request->all();

        $validator = Validator::make($data, [
            $file => 'required|file|image|max:10240',
        ]);

        if ($validator->fails()) {
            $msg = $validator->errors();
            foreach ($msg->all() as $message) {
                $msg = $message;
                break;
            }
            $this->uploadError = $msg;
            return false;
        }

        $temp = $request->input('temp', 0);

        // 获取表单上传文件
        $file = $data[$file];

        if ($temp)
        {
            $path = $file->store('temp/'.date('Ymd'));   
        }
        else
        {
            $path = $file->store(date('Ymd'));   
        }
        // $files = $request->file('imgs');
        if ($path) {
            // return $this->output(0, '', ['url' => env('APP_UPLOAD').$path, 'path' => $path]);
            return $path;
        } else {
            $this->uploadError = '文件上传失败';
            return false;
        }
    }

    public function getUploadError()
    {
        return $this->uploadError;
    }
}
